﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using ConfigGen.Utilities.Extensions.System.Collections.Generic;
using NUnit.Framework;

namespace ConfigGen.Utilities.Tests.Extensions
{
    [TestFixture]
    public class DictionaryExtensionsTests
    {
        [Test]
        public void IncrementCountForKey001_NullDictionaryThrows()
        {
            Dictionary<string, int> dictionary = null;
            Assert.Throws<ArgumentNullException>(() => dictionary.IncrementCountForKey("item1"));
        }

        [Test]
        public void IncrementCountForKey002_NullItemArgument()
        {
            var dictionary = new Dictionary<string, int>();
            Assert.Throws<ArgumentNullException>(() => dictionary.IncrementCountForKey(null));
        }

        [Test]
        public void IncrementCountForKey003_AddNewItem()
        {
            var dictionary = new Dictionary<string, int>();
            dictionary.IncrementCountForKey("item1");

            Assert.AreEqual(1, dictionary.Count, "Incorrect dictionary cout");
            Assert.IsTrue(dictionary.ContainsKey("item1"), "item1 expected in dictionary");
            Assert.AreEqual(1, dictionary["item1"], "Incorrect item1 count");
        }

        [Test]
        public void IncrementCountForKey004_IncrementsExisting()
        {
            var dictionary = new Dictionary<string, int>();
            dictionary.Add("item1", 1);
            dictionary.IncrementCountForKey("item1");

            Assert.AreEqual(1, dictionary.Count, "Incorrect dictionary cout");
            Assert.IsTrue(dictionary.ContainsKey("item1"), "item1 expected in dictionary");
            Assert.AreEqual(2, dictionary["item1"], "Incorrect item1 count");
        }

        [Test]
        public void GetMergedCountDictionary001_DifferentContents()
        {
            var dictionary1 = new Dictionary<string, int>
                                  {
                                      {"item1", 1}
                                  };

            var dictionary2 = new Dictionary<string, int>
                                  {
                                      {"item2", 1}
                                  };

            var mergedDictionary = dictionary1.GetMergedCountDictionary(dictionary2);
            Assert.AreEqual(2, mergedDictionary.Count, "Incorrect count in merged dictionary");
            Assert.IsTrue(mergedDictionary.ContainsKey("item1"), "item 1 expected in merged dictionary");
            Assert.AreEqual(1, mergedDictionary["item1"], "incorrect value for item1");
            Assert.IsTrue(mergedDictionary.ContainsKey("item2"), "item 2 expected in merged dictionary");
            Assert.AreEqual(1, mergedDictionary["item2"], "incorrect value for item2");
        }

        [Test]
        public void GetMergedCountDictionary002_SameContents()
        {
            var dictionary1 = new Dictionary<string, int>
                                  {
                                      {"item1", 1}
                                  };

            var dictionary2 = new Dictionary<string, int>
                                  {
                                      {"item1", 3}
                                  };

            var mergedDictionary = dictionary1.GetMergedCountDictionary(dictionary2);
            Assert.AreEqual(1, mergedDictionary.Count, "Incorrect count in merged dictionary");
            Assert.IsTrue(mergedDictionary.ContainsKey("item1"), "item 1 expected in merged dictionary");
            Assert.AreEqual(4, mergedDictionary["item1"], "incorrect value for item1");
        }

        [Test]
        public void AddKeysToCountDictionary001_NewItems()
        {
            var dictionary = new Dictionary<string, int>();

            dictionary.AddKeysToCountDictionary(new[] {"item2", "item3"});
           
            Assert.AreEqual(2, dictionary.Count, "Incorrect dictionary count");

            Assert.IsTrue(dictionary.ContainsKey("item2"), "item2 expected in dictionary");
            Assert.AreEqual(0, dictionary["item2"], "Incorrect count for item2");

            Assert.IsTrue(dictionary.ContainsKey("item3"), "item3 expected in dictionary");
            Assert.AreEqual(0, dictionary["item3"], "Incorrect count for item3");
        }

        [Test]
        public void AddKeysToCountDictionary002_ExistingItems()
        {
            var dictionary = new Dictionary<string, int>
                                 {
                                     {"item1", 2}
                                 };

            dictionary.AddKeysToCountDictionary(new[] { "item1"});

            Assert.AreEqual(1, dictionary.Count, "Incorrect dictionary count");

            Assert.IsTrue(dictionary.ContainsKey("item1"), "item1 expected in dictionary");
            Assert.AreEqual(2, dictionary["item1"], "Incorrect count for item1");
        }
    }
}
